package com.hdax.dm.listener;

import com.hdax.dm.entity.order.DmOrder;
import com.hdax.dm.service.OrderService;
import com.hdax.dm.service.SubmitOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;

import java.io.IOException;

@RabbitListener(queues = "dm_order_dlx_queue")
@Component
public class OrderMQListener {

    @Autowired
    private SubmitOrderService submitOrderService;
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void checkAndChangeOrder(Channel channel, Message message,String orderNo){
        //手动确认消息
        //1 2 3 4 5 6 8 9
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        byte[] body = message.getBody();
        try {
            DmOrder order = orderService.getOrder(orderNo);
            if(order!=null && order.getOrderType() == 0L){
                //超时未支付
                boolean b = submitOrderService.chageOrderStatus(orderNo, -1L);
                if(b) channel.basicAck(deliveryTag,false);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
